package com.beemans.corekit.utils;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;

import com.anythink.core.api.ATAdInfo;
import com.anythink.nativead.api.ATNativeAdView;
import com.beemans.corekit.constant.ReportEventConstants;
import com.beemans.corekit.integration.topon.ATAdInfoExt;
import com.beemans.corekit.integration.topon.AdvertManager;
import com.beemans.corekit.integration.topon.BannerAdvert;
import com.beemans.corekit.integration.topon.BannerAdvertCallback;
import com.beemans.corekit.integration.topon.BannerAdvertConfig;
import com.beemans.corekit.integration.topon.INativeSelfRender;
import com.beemans.corekit.integration.topon.InsertAdvert;
import com.beemans.corekit.integration.topon.InsertAdvertCallback;
import com.beemans.corekit.integration.topon.InsertAdvertConfig;
import com.beemans.corekit.integration.topon.NativeAdvert;
import com.beemans.corekit.integration.topon.NativeAdvertCallback;
import com.beemans.corekit.integration.topon.NativeAdvertConfig;
import com.beemans.corekit.integration.topon.RewardAdvert;
import com.beemans.corekit.integration.topon.RewardAdvertCallback;
import com.beemans.corekit.integration.topon.RewardAdvertConfig;
import com.beemans.corekit.integration.topon.SplashAdvert;
import com.beemans.corekit.integration.topon.SplashAdvertCallback;
import com.beemans.corekit.integration.topon.SplashAdvertConfig;

public final class TopOnUtils {

    /**
     * 加载开屏广告
     */
    public static void loadSplashAdvert(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull SplashAdvertConfig advertConfig,
            @Nullable ViewGroup viewGroup,
            @Nullable SplashAdvertCallback advertCallback
    ) {
        AdvertManager.loadSplashAdvert(lifecycleOwner, advertConfig, viewGroup, new SplashAdvertCallback() {
            @Override
            public void onAdvertStart(@NonNull SplashAdvert splashAdvert) {
                if (advertCallback != null) {
                    advertCallback.onAdvertStart(splashAdvert);
                }
            }

            @Override
            public void onAdvertRequestPre(@NonNull SplashAdvert splashAdvert, @NonNull ATAdInfoExt adInfoExt) {
                ReportUtils.reportAdvertAdInfoExtEvent(ReportEventConstants.EVENT_AD_SPLASH_REQUEST_PRE, adInfoExt);

                if (advertCallback != null) {
                    advertCallback.onAdvertRequestPre(splashAdvert, adInfoExt);
                }
            }

            @Override
            public void onAdvertRequestAlt(@NonNull SplashAdvert splashAdvert, @NonNull ATAdInfoExt adInfoExt) {
                ReportUtils.reportAdvertAdInfoExtEvent(ReportEventConstants.EVENT_AD_SPLASH_REQUEST_ALT, adInfoExt);

                if (advertCallback != null) {
                    advertCallback.onAdvertRequestAlt(splashAdvert, adInfoExt);
                }
            }

            @Override
            public void onAdvertLoaded(@NonNull SplashAdvert splashAdvert) {
                if (advertConfig.isShowAfterLoaded()) {
                    ReportUtils.reportAdvertAdInfoEvent(ReportEventConstants.EVENT_AD_SPLASH_SHOW_PRE, null);
                }

                if (advertCallback != null) {
                    advertCallback.onAdvertLoaded(splashAdvert);
                }
            }

            @Override
            public void onAdvertLoadFail(@NonNull SplashAdvert splashAdvert, @Nullable String adError) {
                if (advertCallback != null) {
                    advertCallback.onAdvertLoadFail(splashAdvert, adError);
                }
            }

            @Override
            public void onAdvertShow(@NonNull SplashAdvert splashAdvert, @Nullable ATAdInfo atAdInfo) {
                ReportUtils.reportAdvertAdInfoEvent(ReportEventConstants.EVENT_AD_SPLASH_SHOW_ALT, atAdInfo);

                if (advertCallback != null) {
                    advertCallback.onAdvertShow(splashAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertShowFail(@NonNull SplashAdvert splashAdvert) {
                if (advertCallback != null) {
                    advertCallback.onAdvertShowFail(splashAdvert);
                }
            }

            @Override
            public void onAdvertClicked(@NonNull SplashAdvert splashAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertClicked(splashAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertClose(@NonNull SplashAdvert splashAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertClose(splashAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertRevenue(@NonNull SplashAdvert splashAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertRevenue(splashAdvert, atAdInfo);
                }
            }
        });
    }

    /**
     * 加载信息流广告
     */
    public static void loadNativeAdvert(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull NativeAdvertConfig advertConfig,
            @Nullable ATNativeAdView atNativeAdView,
            @Nullable INativeSelfRender nativeSelfRender,
            @Nullable NativeAdvertCallback advertCallback
    ) {
        AdvertManager.loadNativeAdvert(lifecycleOwner, advertConfig, atNativeAdView, nativeSelfRender, new NativeAdvertCallback() {
            @Override
            public void onAdvertStart(@NonNull NativeAdvert nativeAdvert) {
                if (advertCallback != null) {
                    advertCallback.onAdvertStart(nativeAdvert);
                }
            }

            @Override
            public void onAdvertRequestPre(@NonNull NativeAdvert nativeAdvert, @NonNull ATAdInfoExt adInfoExt) {
                ReportUtils.reportAdvertAdInfoExtEvent(ReportEventConstants.EVENT_AD_NATIVE_REQUEST_PRE, adInfoExt);

                if (advertCallback != null) {
                    advertCallback.onAdvertRequestPre(nativeAdvert, adInfoExt);
                }
            }

            @Override
            public void onAdvertRequestAlt(@NonNull NativeAdvert nativeAdvert, @NonNull ATAdInfoExt adInfoExt) {
                ReportUtils.reportAdvertAdInfoExtEvent(ReportEventConstants.EVENT_AD_NATIVE_REQUEST_ALT, adInfoExt);

                if (advertCallback != null) {
                    advertCallback.onAdvertRequestAlt(nativeAdvert, adInfoExt);
                }
            }

            @Override
            public void onAdvertLoaded(@NonNull NativeAdvert nativeAdvert) {
                if (advertConfig.isShowAfterLoaded()) {
                    ReportUtils.reportAdvertAdInfoEvent(ReportEventConstants.EVENT_AD_NATIVE_SHOW_PRE, null);
                }

                if (advertCallback != null) {
                    advertCallback.onAdvertLoaded(nativeAdvert);
                }
            }

            @Override
            public void onAdvertLoadFail(@NonNull NativeAdvert nativeAdvert, @Nullable String adError) {
                if (advertCallback != null) {
                    advertCallback.onAdvertLoadFail(nativeAdvert, adError);
                }
            }

            @Override
            public void onAdvertShow(@NonNull NativeAdvert nativeAdvert, @Nullable ATAdInfo atAdInfo) {
                ReportUtils.reportAdvertAdInfoEvent(ReportEventConstants.EVENT_AD_NATIVE_SHOW_ALT, atAdInfo);

                if (advertCallback != null) {
                    advertCallback.onAdvertShow(nativeAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertShowFail(@NonNull NativeAdvert nativeAdvert) {
                if (advertCallback != null) {
                    advertCallback.onAdvertShowFail(nativeAdvert);
                }
            }

            @Override
            public void onAdvertClicked(@NonNull NativeAdvert nativeAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertClicked(nativeAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertClose(@NonNull NativeAdvert nativeAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertClose(nativeAdvert, atAdInfo);
                }
            }

            @Nullable
            @Override
            public INativeSelfRender createSelfRender(@Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    return advertCallback.createSelfRender(atAdInfo);
                }
                return super.createSelfRender(atAdInfo);
            }

            @Override
            public void onAdvertRevenue(@NonNull NativeAdvert nativeAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertRevenue(nativeAdvert, atAdInfo);
                }
            }
        });
    }

    /**
     * 加载 Banner
     *
     * @noinspection SameParameterValue
     */
    public static void loadBannerAdvert(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull ViewGroup viewGroup,
            @NonNull BannerAdvertConfig advertConfig,
            @Nullable BannerAdvertCallback advertCallback
    ) {
        AdvertManager.loadBannerAdvert(lifecycleOwner, viewGroup, advertConfig, new BannerAdvertCallback() {
            @Override
            public void onAdvertStart(@NonNull BannerAdvert bannerAdvert) {
                if (advertCallback != null) {
                    advertCallback.onAdvertStart(bannerAdvert);
                }
            }

            @Override
            public void onAdvertRequestPre(@NonNull BannerAdvert bannerAdvert, @NonNull ATAdInfoExt adInfoExt) {
                ReportUtils.reportAdvertAdInfoExtEvent(ReportEventConstants.EVENT_AD_BANNER_REQUEST_PRE, adInfoExt);

                if (advertCallback != null) {
                    advertCallback.onAdvertRequestPre(bannerAdvert, adInfoExt);
                }
            }

            @Override
            public void onAdvertRequestAlt(@NonNull BannerAdvert bannerAdvert, @NonNull ATAdInfoExt adInfoExt) {
                ReportUtils.reportAdvertAdInfoExtEvent(ReportEventConstants.EVENT_AD_BANNER_REQUEST_ALT, adInfoExt);

                if (advertCallback != null) {
                    advertCallback.onAdvertRequestAlt(bannerAdvert, adInfoExt);
                }
            }

            @Override
            public void onAdvertLoaded(@NonNull BannerAdvert bannerAdvert) {
                ReportUtils.reportAdvertAdInfoEvent(ReportEventConstants.EVENT_AD_BANNER_SHOW_PRE, null);

                if (advertCallback != null) {
                    advertCallback.onAdvertLoaded(bannerAdvert);
                }
            }

            @Override
            public void onAdvertLoadFail(@NonNull BannerAdvert bannerAdvert, @Nullable String adError) {
                if (advertCallback != null) {
                    advertCallback.onAdvertLoadFail(bannerAdvert, adError);
                }
            }

            @Override
            public void onAdvertShow(@NonNull BannerAdvert bannerAdvert, @Nullable ATAdInfo atAdInfo) {
                ReportUtils.reportAdvertAdInfoEvent(ReportEventConstants.EVENT_AD_BANNER_SHOW_ALT, null);

                if (advertCallback != null) {
                    advertCallback.onAdvertShow(bannerAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertShowFail(@NonNull BannerAdvert bannerAdvert) {
                if (advertCallback != null) {
                    advertCallback.onAdvertShowFail(bannerAdvert);
                }
            }

            @Override
            public void onAdvertClicked(@NonNull BannerAdvert bannerAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertClicked(bannerAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertClose(@NonNull BannerAdvert bannerAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertClose(bannerAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertRevenue(@NonNull BannerAdvert bannerAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertRevenue(bannerAdvert, atAdInfo);
                }
            }
        });
    }

    /**
     * 加载插屏广告
     */
    public static void loadInsertAdvert(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull InsertAdvertConfig advertConfig,
            @Nullable InsertAdvertCallback advertCallback
    ) {
        AdvertManager.loadInsertAdvert(lifecycleOwner, advertConfig, new InsertAdvertCallback() {
            @Override
            public void onAdvertStart(@NonNull InsertAdvert insertAdvert) {
                if (advertCallback != null) {
                    advertCallback.onAdvertStart(insertAdvert);
                }
            }

            @Override
            public void onAdvertRequestPre(@NonNull InsertAdvert insertAdvert, @NonNull ATAdInfoExt adInfoExt) {
                ReportUtils.reportAdvertAdInfoExtEvent(ReportEventConstants.EVENT_AD_INSERT_REQUEST_PRE, adInfoExt);

                if (advertCallback != null) {
                    advertCallback.onAdvertRequestPre(insertAdvert, adInfoExt);
                }
            }

            @Override
            public void onAdvertRequestAlt(@NonNull InsertAdvert insertAdvert, @NonNull ATAdInfoExt adInfoExt) {
                ReportUtils.reportAdvertAdInfoExtEvent(ReportEventConstants.EVENT_AD_INSERT_REQUEST_ALT, adInfoExt);

                if (advertCallback != null) {
                    advertCallback.onAdvertRequestAlt(insertAdvert, adInfoExt);
                }
            }

            @Override
            public void onAdvertLoaded(@NonNull InsertAdvert insertAdvert) {
                if (advertConfig.isShowAfterLoaded()) {
                    ReportUtils.reportAdvertAdInfoEvent(ReportEventConstants.EVENT_AD_INSERT_SHOW_PRE, null);
                }

                if (advertCallback != null) {
                    advertCallback.onAdvertLoaded(insertAdvert);
                }
            }

            @Override
            public void onAdvertLoadFail(@NonNull InsertAdvert insertAdvert, @Nullable String adError) {
                if (advertCallback != null) {
                    advertCallback.onAdvertLoadFail(insertAdvert, adError);
                }
            }

            @Override
            public void onAdvertShow(@NonNull InsertAdvert insertAdvert, @Nullable ATAdInfo atAdInfo) {
                ReportUtils.reportAdvertAdInfoEvent(ReportEventConstants.EVENT_AD_INSERT_SHOW_ALT, null);

                if (advertCallback != null) {
                    advertCallback.onAdvertShow(insertAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertShowFail(@NonNull InsertAdvert insertAdvert) {
                if (advertCallback != null) {
                    advertCallback.onAdvertShowFail(insertAdvert);
                }
            }

            @Override
            public void onAdvertClicked(@NonNull InsertAdvert insertAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertClicked(insertAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertClose(@NonNull InsertAdvert insertAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertClose(insertAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertRevenue(@NonNull InsertAdvert insertAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertRevenue(insertAdvert, atAdInfo);
                }
            }
        });
    }

    /**
     * 加载激励广告
     */
    public static void loadRewardAdvert(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull RewardAdvertConfig advertConfig,
            @Nullable RewardAdvertCallback advertCallback
    ) {
        AdvertManager.loadRewardAdvert(lifecycleOwner, advertConfig, new RewardAdvertCallback() {
            @Override
            public void onAdvertStart(@NonNull RewardAdvert rewardAdvert) {
                if (advertCallback != null) {
                    advertCallback.onAdvertStart(rewardAdvert);
                }
            }

            @Override
            public void onAdvertRequestPre(@NonNull RewardAdvert rewardAdvert, @NonNull ATAdInfoExt adInfoExt) {
                ReportUtils.reportAdvertAdInfoExtEvent(ReportEventConstants.EVENT_AD_REWARD_REQUEST_PRE, adInfoExt);

                if (advertCallback != null) {
                    advertCallback.onAdvertRequestPre(rewardAdvert, adInfoExt);
                }
            }

            @Override
            public void onAdvertRequestAlt(@NonNull RewardAdvert rewardAdvert, @NonNull ATAdInfoExt adInfoExt) {
                ReportUtils.reportAdvertAdInfoExtEvent(ReportEventConstants.EVENT_AD_REWARD_REQUEST_ALT, adInfoExt);

                if (advertCallback != null) {
                    advertCallback.onAdvertRequestAlt(rewardAdvert, adInfoExt);
                }
            }

            @Override
            public void onAdvertLoaded(@NonNull RewardAdvert rewardAdvert) {
                if (advertConfig.isShowAfterLoaded()) {
                    ReportUtils.reportAdvertAdInfoEvent(ReportEventConstants.EVENT_AD_REWARD_SHOW_PRE, null);
                }

                if (advertCallback != null) {
                    advertCallback.onAdvertLoaded(rewardAdvert);
                }
            }

            @Override
            public void onAdvertLoadFail(@NonNull RewardAdvert rewardAdvert, @Nullable String adError) {
                if (advertCallback != null) {
                    advertCallback.onAdvertLoadFail(rewardAdvert, adError);
                }
            }

            @Override
            public void onAdvertShow(@NonNull RewardAdvert rewardAdvert, @Nullable ATAdInfo atAdInfo) {
                ReportUtils.reportAdvertAdInfoEvent(ReportEventConstants.EVENT_AD_REWARD_SHOW_ALT, atAdInfo);

                if (advertCallback != null) {
                    advertCallback.onAdvertShow(rewardAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertShowFail(@NonNull RewardAdvert rewardAdvert) {
                if (advertCallback != null) {
                    advertCallback.onAdvertShowFail(rewardAdvert);
                }
            }

            @Override
            public void onAdvertClicked(@NonNull RewardAdvert rewardAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertClicked(rewardAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertClose(@NonNull RewardAdvert rewardAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertClose(rewardAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertReward(@NonNull RewardAdvert rewardAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertReward(rewardAdvert, atAdInfo);
                }
            }

            @Override
            public void onAdvertRevenue(@NonNull RewardAdvert rewardAdvert, @Nullable ATAdInfo atAdInfo) {
                if (advertCallback != null) {
                    advertCallback.onAdvertRevenue(rewardAdvert, atAdInfo);
                }
            }
        });
    }
}